package com.qnzw.zjmy.home.ui.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.anythink.nativead.api.ATNativeAdView;
import com.qnzw.zjmy.R;
import com.qnzw.zjmy.ad.MediationFeedAd;
import com.qnzw.zjmy.ad.MediationRewardAd;
import com.qnzw.zjmy.ad.MediationTablePlaqueAd;
import com.qnzw.zjmy.ad.inter.RewardAdInter;
import com.qnzw.zjmy.ad.kaku.interstitia.TaKuInterstitialAdSignIn;
import com.qnzw.zjmy.ad.kaku.nativie.TaKuNativeAdSignIn;
import com.qnzw.zjmy.ad.kaku.reward.TaKuRewardAdSignIn;
import com.qnzw.zjmy.config.UrlConfig;
import com.qnzw.zjmy.home.ui.enums.AdStatusEnum;
import com.qnzw.zjmy.home.ui.enums.RedisEnum;
import com.qnzw.zjmy.home.ui.model.User;
import com.qnzw.zjmy.home.ui.model.vo.SignInVo;
import com.qnzw.zjmy.volley.BaseActivity;
import com.qnzw.zjmy.volley.state.HUCState;

import cn.hutool.core.util.StrUtil;


public class SignInActivity extends BaseActivity implements RewardAdInter {
    private TextView tvMoneyRedPacket;
    private TextView tvSignInNumber;
    private View[] llDays;
    private TextView[] tvDayLabels;
    private TextView[] tvDays;
    private String[] moneys;
    private String moneyRedPacket;
    private String signInNumber;
    private String signIdDayNumber;
    private SignInVo baseVo = new SignInVo();

    private MediationTablePlaqueAd mediationTablePlaqueAd;
    private MediationFeedAd mediationFeedAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_sign_in);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("签到");

        //加载数据
        moneyRedPacket = getSharedPreferences("moneyRedPacket");
        signInNumber = getSharedPreferences("signInNumber");
        //连续签到天数
        signIdDayNumber = getSharedPreferences("signIdDayNumber");
        tvMoneyRedPacket.setText(moneyRedPacket);
        tvSignInNumber.setText(signInNumber + "天");
        for (int i = 0; i < Integer.parseInt(signIdDayNumber); i++) {
            llDays[i].setBackgroundResource(R.drawable.radius_orange_kong_2);
            tvDayLabels[i].setTextColor(getResources().getColor(R.color.orange));
            tvDays[i].setTextColor(getResources().getColor(R.color.white));
            tvDays[i].setBackgroundResource(R.drawable.radius_orange);
        }
        String one = getSharedPreferences(RedisEnum.SIGN_IN_ONE.getKey());
        String two = getSharedPreferences(RedisEnum.SIGN_IN_TWO.getKey());
        String three = getSharedPreferences(RedisEnum.SIGN_IN_THREE.getKey());
        String four = getSharedPreferences(RedisEnum.SIGN_IN_FOUR.getKey());
        String five = getSharedPreferences(RedisEnum.SIGN_IN_FIVE.getKey());
        String six = getSharedPreferences(RedisEnum.SIGN_IN_SIX.getKey());
        String seven = getSharedPreferences(RedisEnum.SIGN_IN_SEVEN.getKey());
        String eight = getSharedPreferences(RedisEnum.SIGN_IN_EIGHT.getKey());
        moneys = new String[]{one, two, three, four, five, six, seven, eight};
        double value = Integer.parseInt(one) / 10000.0;
        tvDays[0].setText(value + "元");
        value = Integer.parseInt(two) / 10000.0;
        tvDays[1].setText(value + "元");
        value = Integer.parseInt(three) / 10000.0;
        tvDays[2].setText(value + "元");
        value = Integer.parseInt(four) / 10000.0;
        tvDays[3].setText(value + "元");
        value = Integer.parseInt(five) / 10000.0;
        tvDays[4].setText(value + "元");
        value = Integer.parseInt(six) / 10000.0;
        tvDays[5].setText(value + "元");
        value = Integer.parseInt(seven) / 10000.0;
        tvDays[6].setText(value + "元");
//        initAd();
    }

    @Override
    public void initView() {
        super.initView();
        tvMoneyRedPacket = findViewById(R.id.tv_moneyRedPacket);
        tvSignInNumber = findViewById(R.id.tv_signInNumber);
        llDays = new View[]{findViewById(R.id.ll_day), findViewById(R.id.ll_day1), findViewById(R.id.ll_day2), findViewById(R.id.ll_day3), findViewById(R.id.ll_day4), findViewById(R.id.ll_day5), findViewById(R.id.ll_day6), findViewById(R.id.ll_day7)};
        tvDayLabels = new TextView[]{findViewById(R.id.tv_day_label), findViewById(R.id.tv_day_label1), findViewById(R.id.tv_day_label2), findViewById(R.id.tv_day_label3), findViewById(R.id.tv_day_label4), findViewById(R.id.tv_day_label5), findViewById(R.id.tv_day_label6), findViewById(R.id.tv_day_label7)};
        tvDays = new TextView[]{findViewById(R.id.tv_day), findViewById(R.id.tv_day1), findViewById(R.id.tv_day2), findViewById(R.id.tv_day3), findViewById(R.id.tv_day4), findViewById(R.id.tv_day5), findViewById(R.id.tv_day6), findViewById(R.id.tv_day7)};
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.tv_click_sign).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_click_sign) {
            if (AdStatusEnum.AD_STATUS_YES.getValue().equals(getSharedPreferences(RedisEnum.APP_ANSWER_SIGN_IN.getKey()))) {
                //验证今天看广次数
                String signNumber = getSharedPreferences(RedisEnum.WITHDRAWAL_NUMBER_SIGN.getKey());
                String todayNumber = getSharedPreferences("day_reward_count");
                if (StrUtil.isNotEmpty(signNumber) && StrUtil.isNotEmpty(todayNumber)) {
                    if (Integer.parseInt(todayNumber) < Integer.parseInt(signNumber)) {
                        Toast toast = Toast.makeText(this, "每天答题" + signNumber + "次，可获得1元红包奖励,还差" + (Integer.parseInt(signNumber) - Integer.parseInt(todayNumber)) + "次", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return;
                    }
                }

                String taKu = getSharedPreferences(RedisEnum.TA_KU.getKey());
                AlertDialog loadingDoaLog = showDialogLoading();
                if (RedisEnum.TA_KU.getValue().equals(taKu)) {
                    TaKuRewardAdSignIn taKuRewardAdSignIn = TaKuRewardAdSignIn.getInstance();
                    taKuRewardAdSignIn.setToastContent("看完视频才能领取奖励");
                    taKuRewardAdSignIn.setAlertDialog(loadingDoaLog);
                    taKuRewardAdSignIn.setContext(this);
                    taKuRewardAdSignIn.setRewardAdInter(this);
                    taKuRewardAdSignIn.setToken(getSharedPreferencesToken("token"));
                    taKuRewardAdSignIn.showAd(this);
                } else {
                    MediationRewardAd mediationRewardAd = MediationRewardAd.getInstance();
                    mediationRewardAd.setRewardAdInter(this);
                    mediationRewardAd.setActivity(this);
                    mediationRewardAd.setToastContent("看完视频才能领取奖励");
                    mediationRewardAd.showCacheRewardAd(loadingDoaLog);
                }
            } else {
                signIn(String.valueOf(0));
            }
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        showAdvertisementReward();
        loadDismiss();
    }

    @Override
    public void onFailStrType(int inputType, Object response) {
        super.onFailStrType(inputType, response);
        showToast(response.toString());
        loadDismiss();
    }

    //加载
    private AlertDialog alertDialogRecord;
    private TextView tvContentMoney;

    private void showAdvertisementReward() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        View root = LayoutInflater.from(this).inflate(R.layout.pop_advertisement_reward_record, null);
        dialog.setView(root);
        tvContentMoney = root.findViewById(R.id.tv_content_money);
        tvContentMoney.setText("+" + moneys[Integer.valueOf(signIdDayNumber)]);
        root.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int signIdDayNumberIndex = Integer.parseInt(signIdDayNumber);
                int sumMoney = Integer.parseInt(moneyRedPacket) + Integer.parseInt(moneys[signIdDayNumberIndex]);
                setSharedPreferences("moneyRedPacket", String.valueOf(sumMoney));
                tvMoneyRedPacket.setText(String.valueOf(sumMoney));
                tvSignInNumber.setText(Integer.valueOf(signIdDayNumber) + 1 + "天");
                llDays[signIdDayNumberIndex].setBackgroundResource(R.drawable.radius_orange_kong_2);
                tvDayLabels[signIdDayNumberIndex].setTextColor(getResources().getColor(R.color.orange));
                tvDays[signIdDayNumberIndex].setTextColor(getResources().getColor(R.color.white));
                tvDays[signIdDayNumberIndex].setBackgroundResource(R.drawable.radius_orange);
                alertDialogRecord.dismiss();
            }
        });
        alertDialogRecord = dialog.create();
        alertDialogRecord.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialogRecord.setCancelable(false);
        alertDialogRecord.show();
    }


    @Override
    public void rewardAdSign(String ecpm) {
        signIn(ecpm);
    }

    private void signIn(String ecpm) {
        loading();
        baseVo.setRealityMoney(ecpm);
        baseVo.setToken(getSharedPreferencesToken("token"));
        baseVo.setMoney(moneys[Integer.parseInt(signIdDayNumber)]);
        hucRequestInterfase.executeStr(this, User.class, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_SIGN_IN, hucParameInterfase.getRequestParameter(baseVo), true);
    }

    private void initAd() {
        String taKu = getSharedPreferences(RedisEnum.TA_KU.getKey());
        ATNativeAdView flContent = findViewById(R.id.fl_content);
        if (RedisEnum.TA_KU.getValue().equals(taKu)) {
            TaKuInterstitialAdSignIn taKuInterstitialAdSignIn = TaKuInterstitialAdSignIn.getInstance();
            taKuInterstitialAdSignIn.setToken(getSharedPreferencesToken("token"));
            taKuInterstitialAdSignIn.showAd(this);
            //信息流
            TaKuNativeAdSignIn taKuNativeAdSignIn = TaKuNativeAdSignIn.getInstance();
            taKuNativeAdSignIn.setmATNativeAdView(flContent);
            taKuNativeAdSignIn.setToken(getSharedPreferencesToken("token"));
            taKuNativeAdSignIn.showAd();
        } else {
            mediationTablePlaqueAd = MediationTablePlaqueAd.getInstance();
            mediationTablePlaqueAd.showCacheInterstitialFullAd(this, null);
            //信息流
            mediationFeedAd = MediationFeedAd.getInstance();
            mediationFeedAd.setFlFeed(flContent);
            mediationFeedAd.setActivity(this);
            mediationFeedAd.showCacheFeedAd();
        }
    }
}